<?php

namespace common\model;

use think\model\RelationModel;
use think\module\Module;
use think\Request;

class EntityModel extends RelationModel
{
    // 自动验证
    protected $_validate = [
        ['name', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['name', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['code', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['code', '', '', self::EXISTS_VALIDATE, 'alphaDash'],
        ['project_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['module_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['status_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['entity_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['entity_module_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['start_time', '', '', self::EXISTS_VALIDATE, 'date'],
        ['end_time', '', '', self::EXISTS_VALIDATE, 'date'],
        ['duration', '0,8', '', self::EXISTS_VALIDATE, 'length'],
        ['json', '', '', self::EXISTS_VALIDATE, 'array']
    ];

    // 自动完成
    protected $_auto = [
        ['tenant_id', 'auto_fill_tenant_id', self::MODEL_INSERT, 'function'],
        ['project_id', 'auto_fill_project_id', self::MODEL_INSERT, 'function_with_data'],
        ['module_id', 'auto_fill_default_id', self::MODEL_INSERT, 'function_with_data'],
        ['entity_id', 'auto_fill_default_id', self::MODEL_INSERT, 'function_with_data'],
        ['entity_module_id', 'auto_fill_default_id', self::MODEL_INSERT, 'function_with_data'],
        ['code', 'auto_fill_code', self::MODEL_INSERT, 'function_with_data'],
        ['start_time', 'strtotime', self::EXISTS_VALIDATE, 'function'],
        ['end_time', 'strtotime', self::EXISTS_VALIDATE, 'function'],
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['uuid', 'create_uuid', self::MODEL_INSERT, 'function'],
        ['description', 'fill_text_default_val', self::MODEL_INSERT, 'function']
    ];

    // 自动完成后验证
    protected $_validate_after_auto = [
        ['code,tenant_id,project_id,module_id,entity_id,entity_module_id', '', '', self::MUST_VALIDATE, 'unique'],
    ];

    // 隐藏字段
    protected $hidden = ['json'];

    /**
     * 获取器：自定义字段
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getJsonAttr($value, $data)
    {
        // 当前模块的自定义字段配置
        $currentModuleCode = \request()->getModuleCode();
        $fieldDict = Module::$moduleDictData['field_index_by_code'][$currentModuleCode]['custom'];

        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        // 把自定义值写入追加字段
        foreach ($fieldDict as $filed => $config) {
            if (array_key_exists($filed, $jsonDictData)) {
                $this->appendCustomField[$filed] = $jsonDictData[$filed];
            } else {
                // 没有值赋空值
                $this->appendCustomField[$filed] = "";
            }
        }

        return [];
    }

    /**
     * 获取器：计划开始时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getPlanStartTimeAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：计划结束时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getPlanEndTimeAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }
}
